package br.com.ordersys.framework.db.metadata;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class TableMetadata {
	private String name;
	private String internalName;
	private int type;
	private String description;
	private String criteria;
	private String baseSql;
	private Collection<FieldMetadata> fields;
	private Map<String,RelationMetadata> relations;
	private Map<String, FieldDescriptor> fieldByName;
	private SchemaMetadata schema;
	private int fieldsCount;
	private int keyGenType;
	private String keygenClassName;

	public static interface TYPE {
		int TABLE = 0;
		int VIEW = 1;
	}
	
	public static interface KEY_GEN_TYPE{
		int NONE = 0;
		int SEQUENCE = 1;
		int AUXILIAR_TABLE = 2;
	}

	public TableMetadata(String name, String internalName, String description) {
		this.name = name;
		this.internalName = internalName;
		this.description = description;
		this.type = TYPE.TABLE;
		this.keyGenType = KEY_GEN_TYPE.NONE;
		this.fields = new ArrayList<FieldMetadata>();
		this.relations = new HashMap<String,RelationMetadata>();
		this.fieldByName = new HashMap<String, FieldDescriptor>();
	}

	public void addField(FieldMetadata f) {
		fields.add(f);
		FieldDescriptor fd = new FieldDescriptor();
		fd.index = fieldsCount;
		fd.field = f;
		
		fieldByName.put(f.getName(), fd);

		fieldsCount++;
	}

	public int getType() {
		return type;
	}
	
	public int getFieldIndex(String name) {
		FieldDescriptor fd = fieldByName.get(name);
		
		return fd == null ? -1 : fd.index;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getCriteria() {
		return criteria;
	}

	public void setCriteria(String criteria) {
		this.criteria = criteria;
	}

	public String getBaseSql() {
		return baseSql;
	}

	public void setBaseSql(String baseSql) {
		this.baseSql = baseSql;
	}

	public String getName() {
		return name;
	}

	public String getInternalName() {
		return internalName;
	}

	public String getDescription() {
		return description;
	}

	public Collection<FieldMetadata> getFields() {
		return fields;
	}

	public Map<String,RelationMetadata> getRelations() {
		return relations;
	}

	public void addRelation(RelationMetadata rm) {
		relations.put(rm.getName(),rm);
		rm.setTableMetadata(this);
	}

	public SchemaMetadata getSchema() {
		return schema;
	}

	void setSchema(SchemaMetadata schema) {
		this.schema = schema;
	}

	public int getFieldsCount() {
		return fieldsCount;
	}

	private class FieldDescriptor {
		int index;
		FieldMetadata field;
	}

	public int getKeyGenType() {
		return keyGenType;
	}

	public void setKeyGenType(int keyGenType) {
		this.keyGenType = keyGenType;
	}

	public String getKeygenClassName() {
		return keygenClassName;
	}

	public void setKeygenClassName(String keygenClassName) {
		this.keygenClassName = keygenClassName;
	}
}
